package cn.wolfcode.wolf2w.business.service.impl;

import cn.wolfcode.wolf2w.business.api.RemoteDestinationService;
import cn.wolfcode.wolf2w.business.api.domain.Questionse;
import cn.wolfcode.wolf2w.business.api.domain.Reply;
import cn.wolfcode.wolf2w.business.mapper.QuestionseMapper;
import cn.wolfcode.wolf2w.business.mapper.ReplyMapper;
import cn.wolfcode.wolf2w.business.query.QuestionseQuery;
import cn.wolfcode.wolf2w.business.test.service.IQuestionseService;
import cn.wolfcode.wolf2w.common.core.context.SecurityContextHolder;
import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.member.api.RemoteUserInfoService;
import cn.wolfcode.wolf2w.member.api.domain.UserInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 问答Service业务层处理
 * 
 * @author SQM
 * @date 2024-11-18
 */
@Service
@Transactional
public class QuestionseServiceImpl extends ServiceImpl<QuestionseMapper,Questionse> implements IQuestionseService {

    @Autowired
    private RemoteUserInfoService remoteUserInfoService;

    @Autowired
    private ReplyMapper replyMapper;

    @Override
    public IPage<Questionse> queryPage(QuestionseQuery qo) {
        IPage<Questionse> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        LambdaQueryWrapper<Questionse> qw = new LambdaQueryWrapper<>();
        qw.eq(Questionse::getIsDraft,0);
        IPage<Questionse> questionseIPage = baseMapper.selectPage(page, qw);

        for (Questionse record : questionseIPage.getRecords()) {
            Long id = record.getUserId();
            Reply reply = replyMapper.selectById(id);
            R<UserInfo> inner = remoteUserInfoService.getOne(id, "inner");
            UserInfo data = inner.getData();
            record.setHeadImgUrl(data.getHeadImgUrl());
            record.setAuthorName(data.getNickname());
            record.setReplyId(reply.getQuestionse());
            record.setLevel(data.getLevel());
            record.setDetailDoubt(reply.getDetailDoubt());
//            record.setDestinationName();
        }
        return questionseIPage;

//        return lambdaQuery().eq(Questionse::getIsDraft,0)
//                .page(page);
    }

    @Override
    public void create(Questionse qo) {
        Long uid = SecurityContextHolder.getUserId();
        qo.setUserId(uid);
        baseMapper.insert(qo);
    }
}
